﻿define(function (require, exports, module) {
	var CPDF_TextObject = require('../../PDFData/Text/Reader_TextObject.js');
	var CPDF_TextPage = require('../../PDFData/Text/Reader_TextPage.js');
    var CPDF_Point = FXREADER.CPDF_Point;
    var CPDF_Rect = FXREADER.CPDF_Rect;
    var CommonTools = FXREADER.Common;
    var CMap = FXREADER.CMap;

    /**
    * @name FXREADER.TextSelect
    * @ignore
    * @namespace 这是一个命名空间包含所有有关文本操作类。
    */
    FXREADER.TextSelect = {};

    var SELECT_MSG = function (nStartIndex, nEndIndex) {
        this.nStartIndex = nStartIndex;
        this.nEndIndex = nEndIndex;
    };
    FXREADER.TextSelect.SELECT_MSG = SELECT_MSG;

    FXREADER.TEXT_RETURN_LINEFEED = "\r\n";

    /**
     * @ignore
    * @description 文本页面的选择文本信息类
    * @class 文本页面的选择文本信息类
    * @constructor
    */
    FXREADER.TextSelect.CReader_TextPageSelect = function () {
        var _pTextPage = null,  //pdf文本页面对象
             _SLMsgList = [],   //FXREADER.TextSelect.SELECT_MSG对象数组
             _NeedDrawSLMsgList = [],
             _NeedClearSLMsgList = [];
        /**
        * @description 设置pdf文本页面对象
        * @param {FXREADER.PDFData.Text.CPDF_TextPage} pTextPage pdf文本页面对象
        * @return {FXREADER.PDFData.Text.CPDF_TextPage} 之前的pdf文本页面对象;
        */
        this.SetTextPage = function (pTextPage) {
            var pPage = _pTextPage;
            _pTextPage = pTextPage;
            return pPage;
        };
        /**
        * @description 添加选择区域，依据范围选择区域
        * @param {FXREADER.CPDF_Rect} rcBound 范围选择区域
        */
        this.AddSLRect = function (rcBound) {
            if (!_pTextPage)
                return;
            FXREADER.RectUtils.Normalize(rcBound);
            var bound_array = [];
            _NeedDrawSLMsgList.length = 0;
            _NeedClearSLMsgList.length = 0;
            if (!_pTextPage.GetBoundPageInfo(rcBound, bound_array)) {
                _NeedClearSLMsgList.length = 0;
                _NeedClearSLMsgList = CommonTools.Clone(_SLMsgList);
                _SLMsgList.length = 0;
                _NeedDrawSLMsgList.length = 0;
                return;
            }
            var count = _SLMsgList.length;
            if (count == 0) {
                var size = bound_array.length;
                for (var i = 0; i < size; i++) {
                    var new_info = new SELECT_MSG(-1, -1);
                    new_info.nStartIndex = bound_array[i].start_index;
                    new_info.nEndIndex = bound_array[i].end_index;
                    _SLMsgList.push(new_info);
                }
                _NeedDrawSLMsgList.length = 0;
                _NeedDrawSLMsgList = CommonTools.Clone(_SLMsgList);
                return;
            }
            var bound_map1 = new CMap();
            var bound_map2 = new CMap();
            var map = new CMap();
            var size = bound_array.length;
            for (var i = 0; i < size; i++) {
                var line_index = _pTextPage.GetLineIndexByIndex(bound_array[i].start_index);
                bound_map1.put(line_index, i);
                bound_map2.put(i, line_index);
            }

            for (var j = 0; j < count; j++) {
                var line_index = _pTextPage.GetLineIndexByIndex(_SLMsgList[j].nStartIndex);
                map.put(line_index, j);
                var value = bound_map1.get(line_index);
                if (value != null) {

                    _AddSLRect(_SLMsgList[j], bound_array[value].start_index, bound_array[value].end_index);
                }
                else {
                    _NeedClearSLMsgList.push(CommonTools.Clone(_SLMsgList[j]));
                }
            }

            for (var k = 0; k < size; k++) {
                var line_index = bound_map2.get(k);
                if (line_index == null)
                    return;
                var value = map.get(line_index);
                if (value == null) {
                    var new_info = new SELECT_MSG(-1, -1);
                    new_info.nStartIndex = bound_array[k].start_index;
                    new_info.nEndIndex = bound_array[k].end_index;
                    _NeedDrawSLMsgList.push(new_info);
                }
            }

            _SLMsgList.length = 0;
            for (var m = 0; m < size; m++) {
                var new_info = new SELECT_MSG(-1, -1);
                new_info.nStartIndex = bound_array[m].start_index;
                new_info.nEndIndex = bound_array[m].end_index;
                _SLMsgList.push(new_info);
            }
            return;
        };
        /**
        * @description 添加选择区域，依据字符索引
        * @param {Numeber} start_index 开始字符索引
        * @param {Numeber} end_index 结束字符索引
        */
        this.AddSLRectByIndex = function (start_index, end_index) {
            if (!_pTextPage)
                return;
            if (start_index > end_index || start_index < 0 || end_index > _pTextPage.GetSize()) {
                this.ClearSelectRect();
                return;
            }
            if (_SLMsgList.length == 0) {
                _NeedDrawSLMsgList.length = 0;
                _NeedClearSLMsgList.length = 0;
                var new_info = new SELECT_MSG(-1, -1);
                new_info.nStartIndex = start_index;
                new_info.nEndIndex = end_index;
                _SLMsgList.push(new_info);
                _NeedDrawSLMsgList = CommonTools.Clone(_SLMsgList);
            }
            else {
                var sl_info = _SLMsgList[0];
                _NeedDrawSLMsgList.length = 0;
                _NeedClearSLMsgList.length = 0;
                _AddSLRect(sl_info, start_index, end_index);
                _SLMsgList.length = 0;
                var new_info = new SELECT_MSG(-1, -1);
                new_info.nStartIndex = start_index;
                new_info.nEndIndex = end_index;
                _SLMsgList.push(new_info);
            }
            return;
        };
        
        function _AddSLRect(sl_info, start_index, end_index) {
            if (_pTextPage == null || start_index > end_index)
                return;
            var new_info = new SELECT_MSG(-1, -1);
            //needClear
            if (start_index > sl_info.nStartIndex) {
                if (start_index <= sl_info.nEndIndex) {
                    new_info.nStartIndex = sl_info.nStartIndex;
                    new_info.nEndIndex = start_index - 1;
                    _NeedClearSLMsgList.push(new_info);
                }
                else {
                    _NeedClearSLMsgList.push(sl_info);
                }
            }
            if (end_index < sl_info.nEndIndex) {
                if (end_index >= sl_info.nStartIndex) {
                    new_info.nStartIndex = end_index + 1;
                    new_info.nEndIndex = sl_info.nEndIndex;
                    _NeedClearSLMsgList.push(new_info);
                }
                else {
                    _NeedClearSLMsgList.push(sl_info);
                }
            }
            //needDraw
            if (sl_info.nStartIndex > start_index) {
                if (sl_info.nStartIndex <= end_index) {
                    new_info.nStartIndex = start_index;
                    new_info.nEndIndex = sl_info.nStartIndex - 1;
                    _NeedDrawSLMsgList.push(new_info);
                }
                else {
                    new_info.nStartIndex = start_index;
                    new_info.nEndIndex = end_index;
                    _NeedDrawSLMsgList.push(new_info);
                }
            }
            if (sl_info.nEndIndex < end_index) {
                if (sl_info.nEndIndex >= start_index) {
                    new_info.nStartIndex = sl_info.nEndIndex + 1;
                    new_info.nEndIndex = end_index;
                    _NeedDrawSLMsgList.push(new_info);
                }
                else {
                    new_info.nStartIndex = start_index;
                    new_info.nEndIndex = end_index;
                    _NeedDrawSLMsgList.push(new_info);
                }
            }
            return;
        }
        /**
        * @description 清楚选择区域
        */
        this.ClearSelectRect = function () {
            if (!_pTextPage)
                return;
            _NeedDrawSLMsgList.length = 0;
            _NeedClearSLMsgList.length = 0;
            _NeedClearSLMsgList = CommonTools.Clone(_SLMsgList);
            _SLMsgList.length = 0;
        };
        /**
        * @description 选择文本页面中全部文本
        */
        this.SelectAll = function () {
            if (!_pTextPage)
                return;
            _NeedDrawSLMsgList.length = 0;
            _NeedClearSLMsgList.length = 0;
            _NeedClearSLMsgList = CommonTools.Clone(_SLMsgList);
            var new_info = new SELECT_MSG(-1, -1);
            new_info.nStartIndex = 0;
            new_info.nEndIndex = _pTextPage.GetSize() - 1;
            _SLMsgList.length = 0;
            _SLMsgList.push(new_info);
            _NeedDrawSLMsgList.push(new_info);
        };
        /**
        * @description 是否存在已选择文本
        * @return {Boolean} 存在已选择文本返回true,否则false;
        */
        this.IsSelected = function () {
            return _SLMsgList.length > 0;
        };
        /**
        * @description 获取已选择文本的区域数组
        * @param {Array.<{FXREADER.CPDF_Rect}>} rect_array 区域数组
        * @return {Number} 返回区域数组长度;
        */
        this.GetSelectPDFRect = function (rect_array) {
            rect_array.length = 0;
            var size = _SLMsgList.length;
            for (var i = 0; i < size; i++) {
                var start_index = _SLMsgList[i].nStartIndex;
                var count = _SLMsgList[i].nEndIndex - _SLMsgList[i].nStartIndex + 1;
                if (count <= 0) {
                    continue;
                }
                var tmpRectArr = _pTextPage.GetPDFRect(start_index, count);
                for (var j = 0; j < tmpRectArr.length; j++) {
                    rect_array.push(tmpRectArr[j]);
                }
            }
            return rect_array.length;
        };

        this.GetNeedDrawPDFRect = function (rect_array) {
            rect_array.length = 0;
            var size = _NeedDrawSLMsgList.length;
            for (var i = 0; i < size; i++) {
                var start_index = _NeedDrawSLMsgList[i].nStartIndex;
                var count = _NeedDrawSLMsgList[i].nEndIndex - _NeedDrawSLMsgList[i].nStartIndex + 1;
                if (count <= 0) {
                    continue;
                }
                var tmpRectArr = _pTextPage.GetPDFRect(start_index, count);
                for (var j = 0; j < tmpRectArr.length; j++) {
                    rect_array.push(tmpRectArr[j]);
                }
            }
            return rect_array.length;
        };

        this.GetNeedClearPDFRect = function (rect_array) {
            rect_array.length = 0;
            var size = _NeedClearSLMsgList.length;
            for (var i = 0; i < size; i++) {
                var start_index = _NeedClearSLMsgList[i].nStartIndex;
                var count = _NeedClearSLMsgList[i].nEndIndex - _NeedClearSLMsgList[i].nStartIndex + 1;
                if (count <= 0) {
                    continue;
                }
                rect_array = _pTextPage.GetPDFRect(start_index, count);
            }
            return rect_array.length;
        };

        /*this.GetSelectPDFRectPoint = function(rcPoint_array)
        {
        rcPoint_array.length = 0;
        var size = _SLMsgList.length;
        for (var i=0;i<size;i++)
        {
        var start_index = _SLMsgList[i].nStartIndex;
        var count = _SLMsgList[i].nEndIndex- _SLMsgList[i].nStartIndex+1;
        if(count <= 0)
        {
        continue;
        }
        _pTextPage.GetPDFRectPointInfo(start_index,count,rcPoint_array);
        }
        return rcPoint_array.GetSize();
        }*/
        /**
        * @description 获取已选择文本字符串
        * @return {String} 返回已选择文本字符串;
        */
        this.GetSelectedText = function () {
            var csText = "";
            var size = _SLMsgList.length;
            for (var i = 0; i < size; i++) {
                var start_index = _SLMsgList[i].nStartIndex;
                var count = _SLMsgList[i].nEndIndex - _SLMsgList[i].nStartIndex + 1;
                if (count <= 0) {
                    continue;
                }
                csText += _pTextPage.GetPageText(start_index, count);
                if (i < size - 1) {
                    csText += FXREADER.TEXT_RETURN_LINEFEED;
                }
            }
            return csText;
        };
        /**
        * @description 获取最先的插入符信息
        * @param {FXREADER.PDFData.Text.CARET_INFO} caret_info 返回插入符信息
        */
        this.GetFirstCaretInfo = function (caret_info) {
            caret_info.index = -1;
            if (_SLMsgList.length != 0) {
                _pTextPage.GetCaretByIndex(_SLMsgList[0].nStartIndex, caret_info, true);
            }
        };


    };

    return FXREADER.TextSelect.CReader_TextPageSelect;
});